package org.iabako.client.tools;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import org.iabako.client.navigation.FailureController;
import org.iabako.client.navigation.NavigationController;
import org.iabako.client.ui.layout.field.LabelText;
import org.iabako.client.ui.layout.widget.dialogbox.IabakoDialogBox;
import org.iabako.client.ui.layout.widget.validation.ValidationMessage;
import org.iabako.client.ui.page.user.credit.BuyCredit;
import org.iabako.shared.exception.IabakoException;

/**
 * Created by jose on 16/10/14.
 */
public class IabakoAsyncCallback<T> implements AsyncCallback<T> {

    Timer waitTimer;
    final ValidationMessage messages = ValidationMessage.getFirstFromDOM();
    protected IabakoDialogBox errorDialogBox;

    public IabakoAsyncCallback() {
        waitTimer = new Timer() {
            @Override
            public void run() {
                if (messages != null && messages.isEmpty()) {
                    messages.showAlertMessage("wait_message");
                }
            }
        };
        waitTimer.schedule(300);
    }

    public void onFailure(Throwable caught) {
        waitTimer.cancel();
        if (messages != null) {
            messages.clear("wait_message");
        }

        if (caught instanceof IabakoException) {
            errorDialogBox = new IabakoDialogBox(
                    ((IabakoException) caught).getUiMessageTitle(),
                    ((IabakoException) caught).getUiMessageText(),
                    ((IabakoException) caught).getIconStyle());

            errorDialogBox.setAvoidClosePopupCall(!((IabakoException) caught).isClosePopups());
            errorDialogBox.setCloseOnlySimplePopup(((IabakoException) caught).isCloseOnlySimplePopup());

            if (((IabakoException) caught).isBuyCreditException()) {
                addBuyCreditOptions(errorDialogBox);
            }

            NavigationController.getInstance().show(errorDialogBox);
            return;
        }

        FailureController.handleFailure(caught);
    }

    private void addBuyCreditOptions(final IabakoDialogBox iabakoDialogBox) {
        LabelText upgradeOption = new LabelText("buycreditlink buycreditoption");
        upgradeOption.setStyleClass("buycreditlabel");
        upgradeOption.setLabel("buy_credit_upgrade_option");

        upgradeOption.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                BuyCredit form = new BuyCredit();
                form.setBuyUpgradeView();
                NavigationController.getInstance().closePopups();
                iabakoDialogBox.hide();
                NavigationController.getInstance().goTo(form);
            }
        });

        iabakoDialogBox.addToCustomPanel(upgradeOption);
    }

    public void onSuccess(T result) {
        waitTimer.cancel();
        if (messages != null) {
            messages.clear("wait_message");
        }
    }
}
